#include "dialog_home_find.h"
#include "ui_dialog_home_find.h"

dialog_home_find::dialog_home_find(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dialog_home_find)
{
    ui->setupUi(this);
    this->ui->search_pushButton->setIcon(QIcon(":/音乐搜索"));

    // 固定大小
    this->resize(400,250);
    // 设置窗口图标
    this->setWindowIcon(QIcon(":/音乐弹窗图标"));
    // 输入框占位符
    ui->search_lineEdit->setPlaceholderText("输入歌曲");

    // 按钮优化
    ui->search_pushButton->setFlat(true);

    // 初始化组件
    ui->last_song->setPixmap(QPixmap(":/上一首"));
    ui->last_song->setScaledContents(true);
    ui->next_song->setPixmap(QPixmap(":/下一首"));
    ui->next_song->setScaledContents(true);
    ui->play_song->setPixmap(QPixmap(":/停止"));
    ui->next_song->setScaledContents(true);
    ui->mode_label->setPixmap(QPixmap(":/顺序播放"));
    ui->next_song->setScaledContents(true);

    // 初始化音量滑块，设置初始音量
    ui->volumeSlider->setRange(0, 100);
    ui->volumeSlider->setValue(50);
    // 建立信号链接
    connect(this->ui->volumeSlider, &QSlider::valueChanged, this, &dialog_home_find::onVolumeSliderValueChanged);
    connect(this->ui->last_song, &button_home_miusic::signals_start, this ,[&](bool start){
       Q_UNUSED(start);
       emit last_song_sig();
    });
    connect(this->ui->next_song, &button_home_miusic::signals_start, this ,[&](bool start){
       Q_UNUSED(start);
       emit next_song_sig();
    });
    connect(this->ui->play_song, &button_home_miusic::signals_start, this ,[&](bool start){
       if (start)
       {
           ui->play_song->setPixmap(QPixmap(":/播放"));
           emit play_song_sig();
       }
       else
       {
           ui->play_song->setPixmap(QPixmap(":/停止"));
           emit stop_song_sig();
       }
    });
    connect(this->ui->mode_label, &button_home_miusic::signals_start, this ,[&](bool start){
        if (start)
        {
            this->ui->mode_label->setPixmap(QPixmap(":/随机播放"));
        }
        else
        {
            this->ui->mode_label->setPixmap(QPixmap(":/顺序播放"));
        }
       emit chang_mode_sig(start);
    });

    this->mediaPlayer = NULL;
    // 样式表美化
    this->setStyleSheet(R"(
        QDialog {
            background-color: #f5f5f5;
            border-radius: 8px;
        }
        QLineEdit {
            border: 1px solid #cccccc;
            border-radius: 4px;
            padding: 6px 10px;
            font-size: 14px;
            color: #333333;
        }
        QLineEdit:focus {
            border-color: #409eff;
            outline: none;
        }
        QPushButton {
            background-color: #409eff;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 6px 12px;
            font-size: 14px;
            cursor: pointer;
        }
        QPushButton:hover {
            background-color: #66b1ff;
        }
        QPushButton:pressed {
            background-color: #3a8ee6;
        }
    )");
}

dialog_home_find::~dialog_home_find()
{
    delete ui;
}

// 音乐搜索实现
void dialog_home_find::on_search_pushButton_clicked()
{
    QString search_url_string = QString("http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=") + this->ui->search_lineEdit->text() + QString("&type=1&offset=0&total=true&limit=10");
    this->search_url = QUrl(search_url_string);
    emit this->search_sig(this->search_url);
}

// 设置音量调节器
void dialog_home_find::setMediaPlayer(QMediaPlayer *player)
{
    mediaPlayer = player;
    connect(this->mediaPlayer, &QMediaPlayer::mediaStatusChanged, this , &dialog_home_find::onMediaStatusChanged);
    // 如果播放器不为空，设置滑块当前值为播放器的音量
    if (mediaPlayer)
    {
        ui->volumeSlider->setValue(mediaPlayer->volume());
    }
}

// 滑块值变化时的槽函数，调节媒体播放器音量
void dialog_home_find::onVolumeSliderValueChanged(int value)
{
    if (mediaPlayer)
    {
        mediaPlayer->setVolume(value);
    }
}

void dialog_home_find::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
    Q_UNUSED(status);
    // 当媒体播放到末尾时（EndOfMedia 状态）
    if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
    {
        this->ui->play_song->setPixmap(QPixmap(":/播放"));
        this->ui->play_song->set_start(true);
    }
    if (this->mediaPlayer->state() == QMediaPlayer::StoppedState)
    {
        this->ui->play_song->setPixmap(QPixmap(":/停止"));
        this->ui->play_song->set_start(false);
    }
}
